<?php
namespace backend\models;
use yii\db\ActiveRecord;
/**
 * Created by PhpStorm.
 * User: zhn
 * Date: 2015/8/13
 * Time: 16:18
 */
class Login extends ActiveRecord {
    public $verifyCode;
    public $user;
    public $password;
    public $_user=false;
    public static function tableName(){
        return '{{%admin}}';
    }
    public function rules()
    {
        return [
            [['user','password'],'required',],
            ['password','valiadatePassword'],//自定义函数进行验证
        ];
    }
    //判断密码和账户是否正确
    function valiadatePassword ($attribute, $params) {
        if (!$this->hasErrors()) {
            $user_object=$this->getUser();
            if(!$user_object){
                $this->addError($attribute,'账户或密码不正确');
            }
        }
    }
    //根据用户名和密码查询数据
    public function getUser(){
        if($this->_user===false){
            //这里是使用self调用自身的静态方法
            $this->_user=self::find()->where([
                'user'=>$this->user,
                'password'=>strtoupper(sha1($this->password))
            ])->asArray()->one();
        }
        return $this->_user;
    }
}
?>